package org.lgiavedoni.android.franq.core.dao;

import java.util.Date;
import java.util.UUID;

import lombok.Data;

import org.lgiavedoni.android.franq.util.date.DateUtil;

@Data
public abstract class DaoObject { 

	private final Date creationDate = new Date();
	private UUID id = null;// UUID.randomUUID();

	public Date getCreationDate() {
		return creationDate;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof DaoObject))
			return false;
		return id.compareTo(((DaoObject) obj).getId()) == 0 ? true : false;
	}

	@Override
	public String toString() {
		String sID = id != null ? id.toString() : "NEW";
		return getClass().getSimpleName() + "[" + sID + "]";
	}

	public boolean isSaved() {
		return id != null;
	}

	public void save() {
		if (isSaved()) {
			throw new RuntimeException("This object [" + toString() + "] is already saved");
		}
		id = UUID.randomUUID();
		// creationDate = new Date();
	}

	public String getCreationDateFormated() {
		return DateUtil.parseDateTime(creationDate); 
	}

	public int compareTo(Object object) {
		if (object instanceof DaoObject)
			return -1;
		return getId().compareTo(((DaoObject) object).getId());
	}

}
